#include <iostream>
using namespace std;
#include <cstring>

class Stock {
public:
  void acquire(const char *co, int n, double pr);
  void buy(int num, double price);
  void sell(int num, double price);
  void update(double price);
  void show();

private:
  char company[30];
  int shares;
  double share_val;
  double total_val;
  void set_tot() { total_val = shares * share_val; }
};
//构造函数
void Stock::Stock(const char *co, int n, double pr) {
  strncpy(company, co, 29);
  company[29] = '\0';
  if (n < 0) {
    perror("有误\n");
    exit(1);
  } else {
    shares = n;
    share_val = pr;
    set_tot();
  }
}
void Stock::acquire(const char *co, int n, double pr) {
  strncpy(company, co, 29);
  company[29] = '\0';
  if (n < 0) {
    perror("有误\n");
    exit(1);
  } else {
    shares = n;
    share_val = pr;
    set_tot();
  }
}
void Stock::buy(int num, double price) {
  if (num < 0) {
    perror("err\n"); exit(1);
  } else {
    shares += num;
    share_val = price;
    set_tot();
  }
}

void Stock::sell(int num, double price) {
  if (num < 0) {
    perror("err\n");
    exit(1);
  } else if (num > shares) {
    perror("err shares\n");
  } else {
    shares -= num;
    share_val = price;
    set_tot();
  }
}

void Stock::update(double price) {
  share_val = price;
  set_tot();
}

void Stock::show() {
  cout << "Company:" << company << " Shares:" << shares << endl
       << " Shares Price:" << share_val << " Total Worth:" << total_val << endl;
}

int main() {
 // Stock s1;
 // s1.acquire("NaoSmart", 20, 12.50);
 // cout.setf(ios_base::fixed);
 // cout.precision(2);
 // cout.setf(ios_base::showpoint);
 // s1.show();
 // s1.buy(15, 18.15);
 // s1.show();
 // s1.sell(400, 20.00);
 // s1.show();
 Stock s1 = Stock("lll",22,23.3);
 Stock s2("zzz",33,33.4);
 Stock* p = new Stock("ssss",55,32.5);

  return 0;
}
